package io.gbznt.modules.buss.quartz;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import io.gbznt.modules.buss.entity.BussWeatherRecordEntity;
import io.gbznt.modules.buss.service.BussWeatherRecordService;
import net.sf.json.JSONObject;

public class MyJobweather implements Job {
	@Autowired
    private BussWeatherRecordService bussWeatherRecordService;
	
	String cityid;
	URLConnection connectionData;
	StringBuilder sb;
	BufferedReader br;
	JSONObject jsonData;
	JSONObject info;
    @Override
	public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
    	 JobDataMap dataMap = jobExecutionContext.getJobDetail().getJobDataMap();
         
 		String name = dataMap.getString("parkey");
    	
    	 new Thread(new MyRuns(name)).start();
    	
    }
    
    public MyJobweather() {
        //new的时候注入需要的bean
        this.bussWeatherRecordService = ApplicationContextProvider.getBean(BussWeatherRecordService.class);
    }
    
	/**
	 * 线程执行
	 * 
	 * @author Administrator
	 *
	 */
	class MyRuns implements Runnable {
        String key ;
		
		public MyRuns(String name) {
			super();
			key = name;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
	       // DynamicTask.addJobnot("weather", JOB_GROUP_NAME, "weather", TRIGGER_GROUP_NAME, MyJobweather.class, "0/100 * * * * ?");


	    	String cityid = key;
	        URL url = null;
			try {
				url = new URL("http://t.weather.itboy.net/api/weather/city/"+cityid);
			} catch (MalformedURLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	        try {
				connectionData = url.openConnection();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	        connectionData.setConnectTimeout(1000);
	        try {
				br = new BufferedReader(new InputStreamReader(connectionData.getInputStream(), "utf-8"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        sb = new StringBuilder();
	        String line = null;
	        try {
				while((line = br.readLine()) != null) {
                    sb.append(line);
                }
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        
	        String datas = sb.toString();
	        jsonData = JSONObject.fromObject(datas);
	       // info = jsonData.getJSONObject("weatherinfo");
	       // cal.add(Calendar.DAY_OF_YEAR,i-1);
	        //当前请求日期
	        //更新时间
	        String time = jsonData.getString("time");
	        System.out.println(jsonData.getString("time"));
	        JSONObject city = new JSONObject() ;
	        if(StringUtils.isNotBlank(jsonData.getString("cityInfo"))){
	        	 city = JSONObject.fromObject(jsonData.getString("cityInfo"));
	        }
	        
	        //城市
	        String mycity = city.getString("city");
	        System.out.println(city.getString("city"));
	        System.out.println(jsonData.getString("data"));
	        //当前温度
	        JSONObject data =  new JSONObject();
	        if(StringUtils.isNotBlank(jsonData.getString("data"))){
	        	  data = JSONObject.fromObject(jsonData.getString("data"));
	        }
	       
	        String wendu = data.getString("wendu");
	        //空气质量
	        String airQuality = data.getString("quality");
	        System.out.println(data.getString("wendu"));
	        //最高最低温度
	       // String[] arr = data.getString("forecast").substring(1, data.getString("forecast").length()-1).split(","); 
	        System.out.println(data.getString("forecast"));
	        JSONObject forecast = JSONObject.fromObject(data.getString("forecast").substring(1, data.getString("forecast").length()-1));
	        String high = forecast.getString("high");
	        String low = forecast.getString("low");
	        System.out.println(forecast.getString("high"));
	        System.out.println(forecast.getString("low"));
	        //风向
	        String fx = forecast.getString("fx");
	        String fl = forecast.getString("fl");
	        System.out.println(forecast.getString("fx"));
	        System.out.println(forecast.getString("fl"));
	        //天气情况 晴雨
	        String type = forecast.getString("type");
	        System.out.println(forecast.getString("type"));
	        //星期几
	        String week = forecast.getString("week");
	        System.out.println(forecast.getString("week"));
	        //未来四天
	        //String week = forecast.getString("week");
	        
	        
	        BussWeatherRecordEntity be = new BussWeatherRecordEntity();
	        be.setArea(mycity);
	        be.setAirTemperature(wendu);
	        be.setWeather(type);
	        be.setWindSpeed(fl);
	        be.setFx(fx);
	        be.setWeek(week);
	        be.setHigh(high.substring(2, high.length()));
	        be.setLow(low.substring(2, low.length()));
	        be.setFuture(data.getString("forecast"));
	        be.setAirQuality(airQuality);
	        be.setCreateTime(new Date());
	        
	        bussWeatherRecordService.save(be);
		}
	}
    
    
}